package info.androidhive.pratap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.Request.Method;
import com.android.volley.toolbox.JsonObjectRequest;

import info.androidhive.pratap.models.Album;
import info.androidhive.pratap.models.Product;
import info.androidhive.volleyexamples.R;
import info.androidhive.volleyexamples.app.AppController;
import info.androidhive.volleyexamples.volley.utils.Const;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

public class AlbumsListViewActivity extends Activity {

	private String TAG = AlbumsListViewActivity.class.getSimpleName();
	private ListView dataListView;
	private ProgressDialog pDialog;

	private ArrayList<Album> allAlbums = new ArrayList<Album>();

	// These tags will be used to cancel the requests
	private String tag_json_obj = "jobj_req", tag_json_arry = "jarray_req";
	private JSONArray jsonProuctsArray;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.customlistview_activity);

		dataListView = (ListView) findViewById(R.id.dataListView);

		pDialog = new ProgressDialog(this);
		pDialog.setMessage("Loading...");
		pDialog.setCancelable(false);

		makeJsonObjReq();

	}

	/**
	 * Making json object request
	 * */
	private void makeJsonObjReq() {
		showProgressDialog();
		JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
				Const.GET_ALL_DROPBOX_JSONMUSIC, null,
				new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(JSONObject response) {
						Log.d(TAG, response.toString());

						parseAlbums(response);

						hideProgressDialog();
					}
				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
						VolleyLog.d(TAG, "Error: " + error.getMessage());
						hideProgressDialog();
					}
				}) {

			/**
			 * Passing some request headers
			 * */
			/*
			 * @Override public Map<String, String> getHeaders() throws
			 * AuthFailureError { HashMap<String, String> headers = new
			 * HashMap<String, String>(); headers.put("Content-Type",
			 * "application/json"); return headers; }
			 */

		};

		// Adding request to request queue
		AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);

		// Cancelling request
		// ApplicationController.getInstance().getRequestQueue().cancelAll(tag_json_obj);
	}

	protected void parseAlbums(JSONObject jsonobj) {

		try {

			jsonProuctsArray = jsonobj.getJSONArray("song");

			if (jsonProuctsArray != null) {
				// looping through All albums
				for (int i = 0; i < jsonProuctsArray.length(); i++) {
					JSONObject c = jsonProuctsArray.getJSONObject(i);

					// Storing each json item values in variable
					String id = c.getString("id");
					String title = c.getString("title");
					String artist = c.getString("artist");
					String duration = c.getString("duration");
					String thumb = c.getString("thumb_url");

					// Creating object for each product
					Album singleAlbum = new Album(id, title, artist, duration,
							thumb);

					// adding HashList to ArrayList
					allAlbums.add(singleAlbum);

				}

				AlbumsAdapter padapter = new AlbumsAdapter(this, allAlbums);
				dataListView.setAdapter(padapter);
			} else {
				Log.d("Products: ", "null");
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}

	}

	private void showProgressDialog() {
		if (!pDialog.isShowing())
			pDialog.show();
	}

	private void hideProgressDialog() {
		if (pDialog.isShowing())
			pDialog.hide();
	}

	private void dismissProgressDialog() {
		if (pDialog != null)
			pDialog.dismiss();

	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		dismissProgressDialog();

	}

}
